package gmu.swe.project.registry;

import java.io.Serializable;
import gmu.swe.project.tracker.TrackerInfo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * TrackerRegistration is an object that contains a set of all trackers
 * currently active in the network. The set is updated based on tracker
 * registration initially, and subsequent keep alive messages from the trackers
 * 
 * @author stacy meyer
 */

public class TrackerRegistration implements Serializable {
	ArrayList<TrackerInfo> trackerSet;
	
	public TrackerRegistration() {
		trackerSet = new ArrayList<TrackerInfo>();
	}

	/** Accessors **/
	public ArrayList<TrackerInfo> getTrackerSet() {
		return trackerSet;
	}
	/**
	 * Set the current tracker set to the passed in tracker set
	 * 
	 * @param trackerSet
	 */
	public void setTrackerSet(ArrayList<TrackerInfo> trackerSet) {
		this.trackerSet = trackerSet;
	}// setTrackerSet

	/**
	 * Update a specific tracker in the tracker set, when the same tracker is
	 * found, replace it with the passed in tracker, if no tracker match, then
	 * simply add the tracker to the set
	 * 
	 * @param trackerInfo
	 */
	public void updateTracker(TrackerInfo trackerInfo) {
		// need to remove current data if it's already in the set
		// check the IP and port so that multiple trackers can run on the same server
		String clientName = trackerInfo.getIpAddress();
		Integer clientIP = trackerInfo.getListeningPort();
		Iterator<TrackerInfo> i = this.trackerSet.iterator();
		while (i.hasNext()) {
			TrackerInfo info = (TrackerInfo) i.next();
			if (info.getIpAddress().equals(clientName) && info.getListeningPort().equals(clientIP)) {
				this.trackerSet.remove(info);
				break;
			}
		}
		this.trackerSet.add(trackerInfo);
	}// updateTracker

	/**
	 * Return an iterator of the tracker set
	 * 
	 * @return
	 */
	public Iterator<TrackerInfo> iterator() {
		return this.trackerSet.iterator();
	}// iterator

	/**
	 * Get a specific tracker given the id
	 * 
	 * @param id
	 * @return
	 */
	public TrackerInfo getTracker(String id) {
		TrackerInfo ti = null;
		Iterator<TrackerInfo> i = this.trackerSet.iterator();
		while (i.hasNext()) {
			TrackerInfo info = (TrackerInfo) i.next();
			if (info.getIpAddress().equals(id)) {
				ti = info;
				break;
			}
		}
		return ti;
	}// getTracker

	/**
	 * Helper method to print the ids and timestamp of all the trackers
	 */
	public void printMe() {
		String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dfm = new SimpleDateFormat(DATE_FORMAT_NOW);
		Iterator<TrackerInfo> i = this.trackerSet.iterator();
		System.out.println("TrackerRegistration::current size is: "
				+ Integer.toString(this.trackerSet.size()));
		while (i.hasNext()) {
			TrackerInfo info = (TrackerInfo) i.next();
			System.out.println("TrackerRegistration::tracker id: "
					+ info.getIpAddress());
			System.out.println("TrackerRegistration::tracker timestamp: "
					+ dfm.format(info.getTimestamp()));

		}
		System.out.println("****************************************");
	}// printMe

}// TrackerRegistration
